package com.qf.controller;

import com.alibaba.druid.sql.visitor.functions.If;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package ${PACKAGE_NAME}
 * @Date 2023/4/17 22:13
 */
@WebServlet(name = "GetImagesServlet" ,urlPatterns = "/goodsImgs/*")
public class GetImagesServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        String filename = url.substring(url.lastIndexOf("/") + 1);
        System.out.println(filename);
        String basePath = this.getServletContext().getRealPath("WEB-INF" + File.separator + "images"+File.separator+filename);
        ServletOutputStream os = response.getOutputStream();
        if(filename!=null){
            FileInputStream fis=new FileInputStream(basePath);
            byte[] bytes=new byte[1024];
            int len;
            while ((len=fis.read(bytes))!=-1){
                os.write(bytes,0,len);
            }
            fis.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
